<template>
  <!-- 巡检工单 -->
  <div
    class="workOrder"
    v-for="(item, index) in workBarList"
    :key="item"
    @click="selectWorkType(item, index)"
  >
    <div :class="['workBar', item.type, item.checked ? 'checked' : '']">
      <div class="workIcon"><CopyOutlined /></div>
      {{ item.name }}
      <div class="workNum">{{ item.num }}</div>
    </div>
  </div>
  <div class="filterContent">
    <a-form :model="filterForm" layout="inline">
      <a-form-item label="工单编号">
        <a-input
          v-model:value="filterForm.serialCode"
          class="formInput"
          allow-clear
          placeholder="请输入"
        />
      </a-form-item>
      <a-form-item label="电站名称">
        <a-select
          v-model:value="filterForm.stationId"
          class="formInput"
          placeholder="请选择"
          allow-clear
          show-search
          optionFilterProp="label"
        >
          <a-select-option
            v-for="item in stationtList"
            :value="item.stationId"
            :key="item.stationId"
            :label="item.stationName"
          >
            {{ item.stationName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="省份">
        <a-select
          ref="select"
          v-model:value="filterForm.provId"
          class="formInput"
          placeholder="省份"
          show-search
          allow-clear
          optionFilterProp="label"
          @change="changeProv"
        >
          <a-select-option
            v-for="item in provList"
            :key="item.provId"
            :value="item.provId"
            :label="item.provName"
          >
            {{ item.provName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="城市">
        <a-select
          ref="select"
          v-model:value="filterForm.cityId"
          class="formInput"
          placeholder="城市"
          allow-clear
          @change="changeCity"
          :disabled="!filterForm.provId"
        >
          <a-select-option
            v-for="item in cityList"
            :key="item.cityId"
            :value="item.cityId"
            :label="item.cityName"
          >
            {{ item.cityName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="巡检人">
        <a-input
          v-model:value="filterForm.inspectionUserName"
          class="formInput"
          allow-clear
          placeholder="请输入"
        />
      </a-form-item>
      <a-form-item label="巡检日期">
        <a-range-picker
          v-model:value="filterForm.inspectionDate"
          value-format="YYYY-MM-DD"
          format="YYYY-MM-DD"
          picker="date"
          allowClear
          separator="~"
        />
      </a-form-item>
      <a-button type="primary" class="filterBtn" @click="handleFilter"
        >查询</a-button
      >
      <a-button
        v-hasPermission="'inspectionOrderAdd'"
        type="primary"
        class="filterBtn greenBtn"
        @click="handleAdd"
        >新增</a-button
      >
    </a-form>
  </div>
  <div class="tableContent" ref="tableContainerRef">
    <a-table
      :dataSource="tableData"
      :columns="columns"
      size="small"
      :scroll="tableScroll"
      @resizeColumn="handleResizeColumn"
      :pagination="false"
      :loading="tableLoading"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'workTicketNum'">
          {{ record?.workTicketDtoList?.length || "0" }}
        </template>
        <template v-if="column.key === 'handle'">
          <a-button
            v-if="record.state !== WORK_ORDER_STATUS_ENUM.CLAIM"
            type="link"
            @click="handleInspection(record)"
            v-hasPermission="'inspectionOrder'"
            >巡检表单</a-button
          >
          <a-button
            v-hasPermission="'inspectionOrderTicket'"
            type="link"
            @click="handleTicket(record)"
            >工作票</a-button
          >
          <a-button
            v-hasPermission="'inspectionOrderInfo'"
            type="link"
            @click="handleDetail(record)"
            >详情</a-button
          >
          <a-button
            v-if="
              (selectType === WORK_ORDER_STATUS_ENUM.SUM ||
                selectType === WORK_ORDER_STATUS_ENUM.CLAIM) &&
              record.state === WORK_ORDER_STATUS_ENUM.CLAIM
            "
            v-hasPermission="'inspectionOrderClaim'"
            type="link"
            @click="handleClaim(record)"
            >认领</a-button
          >
          <!--
            inspectionException  1：确认异常 0：异常已处理
            state = 3 待处理时
            如果 inspectionException = 1 说明巡检工单确认-异常 ，需要显示  处理异常 按钮
	          如果 inspectionException = 0 说明没有异常或者处理了异常，不显示 处理异常 按钮
          -->
          <a-button
            v-if="
              (selectType === WORK_ORDER_STATUS_ENUM.SUM ||
                selectType === WORK_ORDER_STATUS_ENUM.HANDLE) &&
              record.state === WORK_ORDER_STATUS_ENUM.HANDLE &&
              record.inspectionException === 1
            "
            type="link"
            @click="handleError(record)"
            v-hasPermission="'inspectionOrderError'"
            >处理异常</a-button
          >
          <a-button
            v-if="
              (selectType === WORK_ORDER_STATUS_ENUM.SUM ||
                selectType === WORK_ORDER_STATUS_ENUM.COMFIRM) &&
              record.state === WORK_ORDER_STATUS_ENUM.COMFIRM
            "
            type="link"
            @click="handleComfirm(record)"
            v-hasPermission="'inspectionOrderConfirm'"
            >确认</a-button
          >
        </template>
      </template>
    </a-table>
    <a-pagination
      v-model:current="filterForm.current"
      v-model:page-size="filterForm.size"
      :total="total"
      position="bottomRight"
      show-size-changer
      @change="getOrderList"
      :show-total="(total) => `总共 ${total} 条`"
    >
      <template #buildOptionText="props">
        <span>{{ props.value }}条/页</span>
      </template>
    </a-pagination>
  </div>
  <!-- 新增工单 -->
  <AddOrder
    :addOrderVisible="addOrderVisible"
    :rowData="rowData"
    :stationtList="stationtList"
    :workOrderType="WORK_ORDER_TYPE.INSPECTION"
    @update:addOrderVisible="(val) => (addOrderVisible = val)"
    @getOrderList="getOrderList"
  />
  <!-- 详情 -->
  <DetailsInfo
    :detailVisible="detailVisible"
    :rowData="rowData"
    :isEdit="isEdit"
    :workOrderType="WORK_ORDER_TYPE.INSPECTION"
    @update:detailVisible="(val) => (detailVisible = val)"
    @getOrderList="getOrderList"
  />
  <!-- 认领工单 -->
  <ClaimOrder
    :claimOrderVisible="claimOrderVisible"
    :rowData="rowData"
    :workOrderType="WORK_ORDER_TYPE.INSPECTION"
    @update:claimOrderVisible="(val) => (claimOrderVisible = val)"
    @getOrderList="getOrderList"
  />
  <!-- 处理异常 -->
  <HandleErr
    :handleErrVisible="handleErrVisible"
    :rowData="rowData"
    :workOrderType="WORK_ORDER_TYPE.INSPECTION"
    @update:handleErrVisible="(val) => (handleErrVisible = val)"
    @getOrderList="getOrderList"
  />
  <!-- 确认工单 -->
  <ConfirmOrder
    :confirmOrderVisible="confirmOrderVisible"
    :rowData="rowData"
    :workOrderType="WORK_ORDER_TYPE.INSPECTION"
    @update:confirmOrderVisible="(val) => (confirmOrderVisible = val)"
    @getOrderList="getOrderList"
  />
  <!-- 巡检表单 -->
  <InspectionForm
    :inspectionFormVisible="inspectionFormVisible"
    :rowData="rowData"
    @update:inspectionFormVisible="(val) => (inspectionFormVisible = val)"
    @getOrderList="getOrderList"
  />
  <!-- 工作票详情 -->
  <TicketInfo
    :ticketInfoVisible="ticketInfoVisible"
    :rowData="rowData"
    :stationtList="stationtList"
    @update:ticketInfoVisible="(val) => (ticketInfoVisible = val)"
    @getOrderList="getOrderList"
  />
</template>
<script setup>
// 
import useTableScroll from "@/hooks/useTableScroll"
import AddOrder from "./conpoments/addOrder.vue"
import DetailsInfo from "./conpoments/detailsInfo.vue"
import ClaimOrder from "./conpoments/claimOrder.vue"
import HandleErr from "./conpoments/handleErr.vue"
import ConfirmOrder from "./conpoments/confirmOrder.vue"
import InspectionForm from "./conpoments/InspectionForm.vue"
import TicketInfo from "./conpoments/ticketInfo.vue"
import { WORK_ORDER_TYPE, WORK_ORDER_STATUS_ENUM } from "@/constants/workOrder"

const $api = inject("$api")
const filterForm = ref({
  serialCode: undefined,
  stationId: undefined,
  provId: undefined,
  cityId: undefined,
  inspectionUserName: undefined,
  inspectionDate: undefined,
  inspectionStartDate: undefined,
  inspectionEndDate: undefined,
  size: 10,
  current: 1,
})
const total = ref()
const detailVisible = ref(false)
const addOrderVisible = ref(false)
const claimOrderVisible = ref(false)
const handleErrVisible = ref(false)
const confirmOrderVisible = ref(false)
const inspectionFormVisible = ref(false)
const ticketInfoVisible = ref(false)
const tableData = ref([])
const isEdit = ref(false)
const rowData = ref({})
const tableLoading = ref(false)
const workBarList = ref([
  {
    name: "工单总数",
    type: "SUM",
    num: 0,
    value: "sum",
    filter: "",
    checked: true,
  },
  {
    name: "待认领工单",
    type: "CLAIM",
    num: 0,
    filter: WORK_ORDER_STATUS_ENUM.CLAIM,
    value: "claim",
    checked: false,
  },
  {
    name: "待处理工单",
    type: "HANDLE",
    num: 0,
    filter: WORK_ORDER_STATUS_ENUM.HANDLE,
    value: "deal",
    checked: false,
  },
  {
    name: "待确认工单",
    type: "COMFIRM",
    num: 0,
    filter: WORK_ORDER_STATUS_ENUM.COMFIRM,
    value: "confirm",
    checked: false,
  },
  {
    name: "已完结工单",
    type: "FINISH",
    num: 0,
    filter: WORK_ORDER_STATUS_ENUM.FINISH,
    value: "finish",
    checked: false,
  },
])
const columns = ref([
  {
    title: "工单编号",
    dataIndex: "serialCode",
    key: "serialCode",
    width: 200,
    resizable: true,
    align: "center",
  },
  {
    title: "电站名称",
    dataIndex: "stationName",
    key: "stationName",
    width: 200,
    resizable: true,
    align: "center",
  },
  {
    title: "省份",
    dataIndex: "provName",
    key: "provName",
    width: 120,
    resizable: true,
    align: "center",
  },
  {
    title: "城市",
    dataIndex: "cityName",
    key: "cityName",
    width: 180,
    resizable: true,
    align: "center",
  },
  {
    title: "工单状态",
    dataIndex: "stateStr",
    key: "stateStr",
    width: 160,
    resizable: true,
    align: "center",
  },
  {
    title: "关联工作票个数",
    dataIndex: "workTicketNum",
    key: "workTicketNum",
    width: 120,
    resizable: true,
    align: "center",
  },
  {
    title: "巡检人",
    dataIndex: "inspectionUserName",
    key: "inspectionUserName",
    width: 180,
    resizable: true,
    align: "center",
  },
  {
    title: "巡检日期",
    dataIndex: "inspectionDate",
    key: "inspectionDate",
    width: 180,
    resizable: true,
    align: "center",
  },
  {
    title: "异常描述",
    dataIndex: "inspectionExceptionDesc",
    key: "inspectionExceptionDesc",
    width: 180,
    resizable: true,
    align: "center",
  },
  {
    title: "工单发起人",
    dataIndex: "createUserName",
    key: "createUserName",
    width: 180,
    resizable: true,
    align: "center",
  },
  {
    title: "工单发起时间",
    dataIndex: "createTime",
    key: "createTime",
    width: 180,
    resizable: true,
    align: "center",
  },
  {
    title: "操作",
    dataIndex: "handle",
    key: "handle",
    //width: 120,
    align: "center",
    fixed: "right",
  },
])
function handleResizeColumn(w, col) {
  col.width = w
}
const handleAdd = () => {
  addOrderVisible.value = true
}
const handleClaim = (data) => {
  rowData.value = data
  claimOrderVisible.value = true
}
const handleError = (data) => {
  rowData.value = data
  handleErrVisible.value = true
}
const handleComfirm = (data) => {
  rowData.value = data
  confirmOrderVisible.value = true
}
const handleInspection = (data) => {
  rowData.value = data
  inspectionFormVisible.value = true
}
const handleTicket = (data) => {
  rowData.value = data
  ticketInfoVisible.value = true
}
const tableContainerRef = ref(null)
const handleFilter = () => {
  filterForm.value.current = 1
  filterForm.value.size = 10
  getOrderList()
}
const provList = ref([])
const cityList = ref([])
const getBaseProvinceTree = async () => {
  const res = await $api.baseProvinceTree()
  provList.value = res?.data
}
const changeProv = async (id) => {
  filterForm.value.cityId = null
  cityList.value = []
  const res = await $api.baseCityTree(id)
  cityList.value = res.data
}
const stationtList = ref([])
const getCompanyStationtList = async () => {
  const res = await $api.companyStationtList()
  if (res?.code === 0) {
    stationtList.value = res.data
  }
}
const handleDetail = (data) => {
  rowData.value = data
  detailVisible.value = true
}
const selectType = ref(WORK_ORDER_STATUS_ENUM.SUM)
const selectWorkType = (item, index) => {
  selectType.value = item.filter
  for (const item of workBarList.value) {
    item.checked = false
  }
  workBarList.value[index].checked = true
  getOrderList(true)
}
const getOrderList = async (isFilter = false) => {
  const { inspectionDate, ...rest } = filterForm.value
  const params = {
    ...rest,
    type: WORK_ORDER_TYPE.INSPECTION,
  }
  if (filterForm.value.inspectionDate?.length) {
    params.inspectionStartDate = filterForm.value.inspectionDate[0]
    params.inspectionEndDate = filterForm.value.inspectionDate[1]
  }
  if (selectType.value !== WORK_ORDER_TYPE.SUM) {
    params.state = workBarList.value.find((item) => item.checked).filter
  }
  tableLoading.value = true
  if (!isFilter) {
    // 通过顶部工单状态点击筛选，不需要请求工单数量接口
    getWorkOrderCount(params)
  }
  const res = await $api.workOrderList(params)
  tableLoading.value = false
  if (res && res.code === 0) {
    tableData.value = res.data.records
    total.value = res.data.total
  }
}
const getWorkOrderCount = async (params) => {
  const res = await $api.workOrderCount(params)
  if (res && res.code === 0) {
    for (const item of workBarList.value) {
      item.num = res.data[item.value] || "0"
    }
  }
}
onMounted(async () => {
  await getOrderList()
  getBaseProvinceTree()
  getCompanyStationtList()
})
const { tableScroll } = useTableScroll(tableData, tableContainerRef)
</script>

<style lang="less" scoped>
.ant-table {
  .ant-btn {
    padding: 4px 5px;
  }
}
.formInput {
  width: 155px;
}
.filterBtn {
  margin: 0 20px 20px 0;
  &:last-child {
    margin-right: 0;
  }
}
.tableContent {
  border-radius: 4px;
  background-color: #141414;
  padding: 8px;
}
.workOrder {
  margin: 10px 20px 20px 0;
  display: inline-block;
  cursor: pointer;
  .workBar {
    width: 180px;
    height: 55px;
    line-height: 55px;
    position: relative;
    border-radius: 4px;
  }
  .workIcon {
    width: 28px;
    height: 28px;
    line-height: 28px;
    text-align: center;
    display: inline-block;
    border-radius: 50%;
    margin: 0 15px 0 10px;
  }
  .workNum {
    margin-left: 20px;
    position: absolute;
    right: 8px;
    top: 0px;
  }
  .SUM {
    border: 1px solid #1990ff;
    .workNum {
      color: rgba(25, 144, 255);
    }
    .workIcon {
      background: rgba(25, 144, 255);
    }
    .checked {
      background: rgba(25, 144, 255, 0.3);
    }
    &[class*=" checked"] {
      background: rgba(25, 144, 255, 0.3);
    }
  }
  .CLAIM {
    border: 1px solid #30c25b;
    .workNum {
      color: #30c25b;
    }
    .workIcon {
      background: #30c25b;
    }
    &[class*=" checked"] {
      background: rgba(48, 194, 91, 0.3);
    }
  }
  .HANDLE {
    border: 1px solid rgb(250 204 20);
    .workNum {
      color: rgb(250 204 20);
    }
    .workIcon {
      background: rgb(250 204 20);
    }
    &[class*=" checked"] {
      background: rgba(250, 204, 20, 0.3);
    }
  }
  .COMFIRM {
    border: 1px solid rgb(22 194 195);
    .workNum {
      color: rgb(22 194 195);
    }
    .workIcon {
      background: rgb(22 194 195);
    }
    &[class*=" checked"] {
      background: rgba(22, 194, 195, 0.3);
    }
  }
  .FINISH {
    border: 1px solid rgba(240, 72, 100, 1);
    .workNum {
      color: rgba(240, 72, 100, 1);
    }
    .workIcon {
      background: rgba(240, 72, 100, 1);
    }
    &[class*=" checked"] {
      background: rgba(240, 72, 100, 0.3);
    }
  }
}
</style>
